package org.awa.demo.opencv.gltf.handler;


import lombok.extern.slf4j.Slf4j;
import org.awa.demo.opencv.gltf.Constant;
import org.awa.demo.opencv.gltf.SoundUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

//水平面
@Slf4j
@Component
public class FloorHandler implements IRectangleHandler{

    //以左下、右下、右上、左上的规则获取轮廓点列表
    @Override
    public List<float[]> getRectanglePointList(List<float[]> pointList) {
        //根据地面特性，Z应该是固定的。
        // 所以根据x的最大最小值可以直接拿到对应y的值。
        // 面的四个点分别是[minX,minY,minZ],[maxX,maxY,minZ],[minX,minY,maxZ],[maxX,maxY,maxZ]
        float maxX = Constant.MIN_FLOAT;
        float minX = Constant.MAX_FLOAT;
        float maxY = Constant.MIN_FLOAT;
        float minY = Constant.MAX_FLOAT;
        float z = pointList.get(0)[2];
        for (int i = 0; i < pointList.size(); i++) {
            float[] point  = pointList.get(i);
            float x = point[0];
            float y = point[1];

            if(maxX < x){
                maxX = x;
            }
            if(minX > x){
                minX = x;
            }
            if(maxY < y){
                maxY = y;
            }
            if(minY > y){
                minY = y;
            }
        }

        List<float[]> resultList = new ArrayList<>();
        resultList.add(new float[]{
                minX,maxY,z
        });
        resultList.add(new float[]{
                maxX,maxY,z
        });
        resultList.add(new float[]{
                maxX,minY,z
        });
        resultList.add(new float[]{
                minX,minY,z
        });

        return resultList;
    }

    //以指定水平垂直格数拆分面为网格集合，并返回各网格的中心点坐标
    @Override
    public List<float[]> getGridObject(List<float[]> pointList, int widthSize, int heightSize) {
        float[] firstPoint = pointList.get(0);
        float z = firstPoint[2];

        float[] xArr = SoundUtil.getPointListByLine(pointList.get(0)[0],pointList.get(1)[0],widthSize);
        float[] yArr = SoundUtil.getPointListByLine(pointList.get(1)[1],pointList.get(2)[1],heightSize);

        List<float[]> resultList = new ArrayList<>();

        for (int i = 0; i < yArr.length; i++) {
            float y = yArr[i];
            for (int j = 0; j < xArr.length; j++) {
                float x = xArr[j];
                resultList.add(new float[]{x,y,z});
            }
        }
        return resultList;
    }


}
